<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="dataList" :loading="loading"
        @selection-change="selectionChange">
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="handleCurrentChange" :page="page" :total="total">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEdit :title="'直鱼'" :batchList="batchList" :fishPondList="fishPondList" :fishVarietyList="fishVarietyList"
        :specificationList="specificationList" :staffList="staffList" @closeDialog="closeDialog"
        :addEditFlag="addEditFlag" :dialogVisible="dialogVisible" :data="formData">
      </AddEdit>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import TableData from '@/components-production-management/TableData';
import BtnSlot from '@/components-production-management/BtnSlot';
import EnumSlot from '@/components-production-management/EnumSlot';
import AddEdit from './components/addEdit.vue';

//批次
import batchManagement from '@/api/production-management/batch-management.js';
//鱼塘
import fishPondApi from "@/api/basic-info-manage/fishPond";
//直鱼接口
import deadRecord from '@/api/production-management/dead-record.js';
//品种
import fishVarietyApi from "@/api/basic-info-manage/fishVariety"
//规格
import specificationApi from "@/api/basic-info-manage/specification/index";
//获取员工
import staffApi from "@/api/basic-info-manage/staff";
export default {
  components: { MainSlot, Pagination, SearchForm, TableData, BtnSlot, EnumSlot, AddEdit },
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      dialogVisible: false,
      dialogVisible_lookView: false,
      addEditFlag: 'add',
      selectList: [],
      unitList: [],
      formData: {},
      page: 1,
      total: 10,
      selectIndex: 0,
      searchFormData: {},
      btnList: ['add', 'delete'],
      dataList: [],
      tableThead: [
        { isShow: true, label: '批次编号', prop: 'batchName' },
        { isShow: true, label: '养殖鱼塘', prop: 'fishPondName' },
        { isShow: true, label: '称重品种', prop: 'fishVarietyName' },
        { isShow: true, label: '记录人', prop: 'staffName' },
        { isShow: true, label: '直鱼数量(尾)', prop: 'num' },
        { isShow: true, label: '直鱼重量(斤)', prop: 'weight' },
        { isShow: true, label: '直鱼日期', prop: 'deadDate' }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,

        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'del',
              handleRow: this.handleRow
            }
          ]
        },
      },
      formItemList: [
        {
          label: '养殖鱼塘',
          type: 'select',
          selectOptions: [],
          param: 'fishPondId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '养殖批次',
          type: 'select',
          selectOptions: [],
          param: 'productionBatchId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '记录人',
          type: 'select',
          selectOptions: [],
          param: 'staffId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '记录日期',
          type: 'datetimerange',
          param: 'dateTime',
          multiple: false,
          isShow: true
        },
      ],
      fishPondList: [],
      batchList: [],
      allbatchList: [],
      staffList: [],
      specificationList: [],
      fishVarietyList: [],
      breedTypeList: [],
      breedTypeObj: {}
    }
  },
  mounted() {
    this.queryPageByCondition();
    this.initData();
  },
  methods: {
    //初始化数据
    async initData() {
      let { data: fishPondList } = await fishPondApi.queryByCondition({ bindBatch: true });//查询鱼塘列表 //bindBatch:true 查询绑定了批次的鱼塘
      let { data: batchList } = await batchManagement.queryByCondition({ unCompleteBatch: true });//查询批次列表
      let { data: allbatchList } = await batchManagement.queryByCondition();
      let { data: fishVarietyList } = await fishVarietyApi.queryByCondition();//查询品种
      let { data: specificationList } = await specificationApi.queryByCondition();//查询规格
      let { data: staffList } = await staffApi.queryByCondition()
      this.fishPondList = fishPondList || [];
      this.batchList = batchList || [];
      this.allbatchList = allbatchList || [];
      this.fishVarietyList = fishVarietyList || [];
      this.specificationList = specificationList || [];
      this.staffList = staffList || [];
      this.setFormOptions();
    },
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = []
      list.forEach((item, index) => {
        if (item.param.indexOf('fishPondId') !== -1) {
          let selectOptions = this.fishPondList.map(data => {
            return {
              text: data.pondName, value: data.fishPondId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('fishVarietyId') !== -1) {
          let selectOptions = this.fishVarietyList.map(data => {
            return {
              text: data.varietyName, value: data.fishVarietyId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('productionBatchId') !== -1) {
          let selectOptions = this.allbatchList.map(data => {
            return {
              text: data.batchName, value: data.batchId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('staffId') !== -1) {
          let selectOptions = this.staffList.map(data => {
            return {
              text: data.staffName, value: data.staffId
            }
          })
          item.defaultSelect = this.searchFormData[item.param];
          item.selectOptions = selectOptions;
        };
      });
      this.$nextTick(() => {
        this.$set(this, 'formItemList', list)
        console.log(this.formItemList)
      })
    },
    //批量删除
    delList(deleteList = [], key = 'deadRecordId') {
      let list = []
      deleteList.forEach(item => {
        list.push(item[key])
      })
      console.log(list)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let res = await deadRecord.delListById(list);
        if (res.success) {
          this.$message.success(res.message || '删除成功');
          this.queryPageByCondition();
        } else {
          this.$message.warning(res.message || '删除失败');
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //查询
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: 10,
        entity: {
          ...this.searchFormData
        }
      }
      this.loading = true;
      deadRecord.queryPageByCondition(params).then(res => {
        this.loading = false;
        if (res.success == 1) {
          this.dataList = res.data.content || [];
          this.total = res.data.totalElements;
          if (res.data.totalPages && this.page > res.data.totalPages) {
            this.page = res.data.totalPages;
            this.queryPageByCondition();
          }
        } else {
          this.$message.warning(res.message || '分页查询失败')
        }
      }).catch(err => {
        this.loading = false;
        console.log(err)
      })
    },
    //搜索
    search(data) {
      this.page = 1;
      let endData = null;
      let startData = null;
      if (data.dateTime && data.dateTime.length == 2) {
        startData = data.dateTime[0];
        endData = data.dateTime[1];
      }
      this.searchFormData = {
        ...this.searchFormData,
        ...data,
        deadEndDate: endData,
        deadStartDate: startData
      };
      this.queryPageByCondition();
    },
    //翻页查询
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    //查看详情
    lookView(row) {
      console.log(row)
      this.dialogVisible_lookView = true;
    },
    // table操作方法
    async handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.dialogVisible = await this.$getBatchStatus(rowData.productionBatchId);
        this.formData = JSON.parse(JSON.stringify(rowData))
      } else if (item.flag == 'del') {
        this.delList([rowData]);
      }
      this.addEditFlag = item.flag
    },
    // 多选方法
    selectionChange(val) {
      this.selectList = val || []
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisible = true
      } else if (flag == 'delete') {
        if (this.selectList.length > 0) {
          this.delList(this.selectList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisible = false;
      this.queryPageByCondition();
    },
  }
};
</script>
<style lang="scss" scoped></style>